
package com.hlkj.pay.service.finance.impl;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.hlkj.pay.dto.finance.MerchantFinanceRecordPageQueryDto;
import com.hlkj.pay.infrastructure.mapper.finance.MerchantFinanceRecordMapper;
import com.hlkj.pay.infrastructure.model.finance.MerchantFinanceRecordDO;
import com.hlkj.pay.service.CommonSnFilterService;
import com.hlkj.pay.service.finance.IMerchantFinanceRecordService;
import com.hlkj.pay.vo.finance.req.MerchantFinanceRecordReq;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author HlpayTeam
 * @date 2024/11/14 16:01
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class MerchantFinanceRecordServiceImpl extends CommonSnFilterService implements IMerchantFinanceRecordService {

    private final MerchantFinanceRecordMapper merchantFinanceRecordMapper;

    @Override
    public PageResult<MerchantFinanceRecordDO> queryPage(MerchantFinanceRecordPageQueryDto financeRecordPageQueryDto) {
        return merchantFinanceRecordMapper.selectPage(financeRecordPageQueryDto, buildWrapper(financeRecordPageQueryDto));
    }

    @Override
    public List<MerchantFinanceRecordDO> queryList(MerchantFinanceRecordPageQueryDto financeRecordPageQueryDto) {
        return merchantFinanceRecordMapper.selectList(buildWrapper(financeRecordPageQueryDto));
    }

    @Override
    public void addFinanceRecord(MerchantFinanceRecordDO merchantFinanceRecordDO) {
        merchantFinanceRecordMapper.insert(merchantFinanceRecordDO);
    }

    @Override
    public MerchantFinanceRecordDO getMerchantFinanceRecordDO(MerchantFinanceRecordReq merchantFinanceRecordReq) {
        LambdaQueryWrapperX<MerchantFinanceRecordDO> lambdaQueryWrapper = new LambdaQueryWrapperX<>();
        lambdaQueryWrapper.eqIfPresent(MerchantFinanceRecordDO::getSn, merchantFinanceRecordReq.getSn());
        lambdaQueryWrapper.eqIfPresent(MerchantFinanceRecordDO::getOrderNo, merchantFinanceRecordReq.getOrderNo());
        lambdaQueryWrapper.eqIfPresent(MerchantFinanceRecordDO::getType, merchantFinanceRecordReq.getType());
        lambdaQueryWrapper.eqIfPresent(MerchantFinanceRecordDO::getChangeType, merchantFinanceRecordReq.getChangeType());
        lambdaQueryWrapper.last(" limit 1");

        return merchantFinanceRecordMapper.selectOne(lambdaQueryWrapper);
    }

    private LambdaQueryWrapperX<MerchantFinanceRecordDO> buildWrapper(MerchantFinanceRecordPageQueryDto financeRecordPageQueryDto) {
        filterSn(financeRecordPageQueryDto);
        LambdaQueryWrapperX<MerchantFinanceRecordDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.likeIfPresent(MerchantFinanceRecordDO::getOrderNo, financeRecordPageQueryDto.getOrderNo());
        queryWrapper.likeIfPresent(MerchantFinanceRecordDO::getFinanceId, financeRecordPageQueryDto.getFinanceId());
        queryWrapper.eqIfPresent(MerchantFinanceRecordDO::getChangeType, financeRecordPageQueryDto.getChangeType());
        queryWrapper.eqIfPresent(MerchantFinanceRecordDO::getType, financeRecordPageQueryDto.getType());
        queryWrapper.eqIfPresent(MerchantFinanceRecordDO::getSn, financeRecordPageQueryDto.getSn());
        queryWrapper.inIfPresent(MerchantFinanceRecordDO::getSn, financeRecordPageQueryDto.getSnList());
        queryWrapper.eqIfPresent(MerchantFinanceRecordDO::getSysType, financeRecordPageQueryDto.getSysType());

        queryWrapper.geIfPresent(MerchantFinanceRecordDO::getAmount, financeRecordPageQueryDto.getMinAmount());
        queryWrapper.leIfPresent(MerchantFinanceRecordDO::getAmount, financeRecordPageQueryDto.getMaxAmount());
        queryWrapper.geIfPresent(MerchantFinanceRecordDO::getCreateTime, financeRecordPageQueryDto.getStartQueryTime());
        queryWrapper.leIfPresent(MerchantFinanceRecordDO::getCreateTime, financeRecordPageQueryDto.getEndQueryTime());
        return queryWrapper;
    }
}
